//
//  SettingViewController.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/2.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "SettingViewController.h"
#import "PublicGetPushConfigApi.h"
#import "PublicSetPushConfigApi.h"

@interface SettingViewController ()

/** <#Description#> **/
@property(nonatomic,strong) NSArray *titleList;
@property(nonatomic,strong) UIButton *soundButton;
@property(nonatomic,strong) UIButton *vibrateButton;
@property(nonatomic,strong) UIButton *notificationButton;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)refreshBaseControlValue {
    
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    [self setNavigationTitle:@"设置" titleColor:[UIColor blackColor]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"D0D0D0"];
    
    [self masLayoutSubviews];
    
    [self getPushConfig];
}

#pragma mark -- 事件
- (void)getPushConfig{
    PublicGetPushConfigApi *api = [[PublicGetPushConfigApi alloc] init];
    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary *dic = [api responseJSONObject];
        if ([dic[@"errcode"] intValue] == 0) {
            if ([dic[@"data"][@"is_push"] isEqualToString:@"1"]) {
                self.notificationButton.selected = YES;
            } else {
                self.notificationButton.selected = NO;
            }
        }else{
            [SVProgressHUD showInfoWithStatus:dic[@"errmsg"]];
        }
    } failure:nil];
    
}

#pragma mark -- 点击事件
- (void)didClickSoundButton {
    self.soundButton.selected = !self.soundButton.selected;
}

- (void)didClickVibrateButton {
    self.vibrateButton.selected = !self.vibrateButton.selected;
    if (self.vibrateButton.selected) {
        DTCSAVE_OBJECT(@"YES", @"Vibrate");
    } else {
        DTCSAVE_OBJECT(@"NO", @"Vibrate");
    }
}

- (void)didClickNotificationButton {
    NSString *is_push;
    if (self.notificationButton.selected) {
        is_push = @"0";
    } else {
        is_push = @"1";
    }
    PublicSetPushConfigApi *api = [[PublicSetPushConfigApi alloc] initWithDic:@{@"is_push":is_push}];
    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary *dic = [api responseJSONObject];
        if ([dic[@"errcode"] intValue] == 0) {
            self.notificationButton.selected = !self.notificationButton.selected;
        }else{
            [SVProgressHUD showInfoWithStatus:dic[@"errmsg"]];
        }
    } failure:^(YTKBaseRequest * _Nonnull request) {
        
    }];
}

#pragma mark -- 懒加载
- (NSArray *)titleList
{
    if(!_titleList)
    {
        _titleList = @[@"振动",@"接受消息推送"];
    }
    return _titleList;
}

- (UIButton *)soundButton
{
    if(!_soundButton)
    {
        UIView *superView = self.view;
        _soundButton = [[UIButton alloc]init];
        [superView addSubview:_soundButton];
        [_soundButton setImage:[UIImage imageNamed:@"but_off"] forState:UIControlStateNormal];
        [_soundButton setImage:[UIImage imageNamed:@"but_on"] forState:UIControlStateSelected];
        [_soundButton addTarget:self action:@selector(didClickSoundButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _soundButton;
}
- (UIButton *)vibrateButton
{
    if(!_vibrateButton)
    {
        UIView *superView = self.view;
        _vibrateButton = [[UIButton alloc]init];
        [superView addSubview:_vibrateButton];
        [_vibrateButton setImage:[UIImage imageNamed:@"but_off"] forState:UIControlStateNormal];
        [_vibrateButton setImage:[UIImage imageNamed:@"but_on"] forState:UIControlStateSelected];
        [_vibrateButton addTarget:self action:@selector(didClickVibrateButton) forControlEvents:UIControlEventTouchUpInside];
        if ([DTCEXRACT_OBJECT(@"Vibrate") isEqualToString:@"YES"] || DTCEXRACT_OBJECT(@"Vibrate") == nil) {
            _vibrateButton.selected = YES;
        } else {
            _vibrateButton.selected = NO;
        }

    }
    return _vibrateButton;
}
- (UIButton *)notificationButton
{
    if(!_notificationButton)
    {
        UIView *superView = self.view;
        _notificationButton = [[UIButton alloc]init];
        [superView addSubview:_notificationButton];
        [_notificationButton setImage:[UIImage imageNamed:@"but_off"] forState:UIControlStateNormal];
        [_notificationButton setImage:[UIImage imageNamed:@"but_on"] forState:UIControlStateSelected];
        [_notificationButton addTarget:self action:@selector(didClickNotificationButton) forControlEvents:UIControlEventTouchUpInside];

    }
    return _notificationButton;
}

#pragma mark -- 初始化
- (void) masLayoutSubviews{
    int count = 2;
    UIView *lastView = nil;
    NSArray *array = @[self.vibrateButton,self.notificationButton];
    for(int i = 0;i < count;++i ){
        //创建一个View
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view];
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor colorWithHexString:@"#D0D3DC"];
        
        UILabel *title = [[UILabel alloc] init];
        title.font = [UIFont systemFontOfSize:15];
        title.textColor = [UIColor colorWithHexString:@"#585D6D"];
        title.text = self.titleList[i];
        
        
        [view addSubview:line];
        [view addSubview:title];
        //向subView添加约束
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self.view);
            make.height.mas_equalTo(px1920Height(200));
            //判断是否有前一个子View
            if ( lastView ) {
                //如果有前一个View，上边界和前一个View的下边界紧贴
                make.top.mas_equalTo(lastView.mas_bottom);
            } else {
                //如果没有前一个View，上边界和container的下边界紧贴
                make.top.mas_equalTo(self.view.mas_top).with.offset(64);
            }
        }];
        
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(view);
            make.width.equalTo(self.view);
            make.height.mas_equalTo(1);
        }];
        
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(line).with.offset(-px1920Height(100));
            make.width.mas_equalTo(px1080Width(350));
            make.height.mas_equalTo(px1080Width(100));
            make.left.equalTo(self.view).with.offset(px1080Width(52));
        }];
        
        UIButton *button = array[i];
        [view addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(line).with.offset(-px1920Height(100));
            //            make.left.equalTo(title.mas_right);
            make.right.equalTo(view).with.offset(-px1920Height(50));
            make.height.mas_equalTo(px1920Height(200));
        }];
        //保存前一个View
        lastView = view;
    }
    
    
}


@end
